package com.xindaoapp.happypet.fragment;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.MyPostAdapter;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.api.Address;
import com.xindaoapp.happypet.bean.Message;
import com.xindaoapp.happypet.bean.MyHuiFu;
import com.xindaoapp.happypet.ui.PostDetailActivity;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.view.pulltorefresh.PullToRefreshBase;
import com.xindaoapp.happypet.view.pulltorefresh.PullToRefreshBase.OnRefreshListener;
import com.xindaoapp.happypet.view.pulltorefresh.PullToRefreshListView;

public class OhterTab2Fragment extends XDBaseFragment {

    private String uid = "";

    private Context mContext;
    private PullToRefreshListView listView;

    private FrameLayout fl;



    public static OhterTab2Fragment Instance(Bundle args) {

        final OhterTab2Fragment f = new OhterTab2Fragment();

        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        uid = getArguments() != null ? getArguments().getString("uid") : "";
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return mView = inflater.inflate(R.layout.fragment_other_tab2, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private int currentpage = 1;

    @Override
    protected void initUi() {
        loadData(currentpage);

        mContext = getActivity();

        fl = (FrameLayout) findViewById(R.id.loading);

        listView = (PullToRefreshListView) findViewById(R.id.listview);


        listView.setPullRefreshEnabled(false);
        listView.setScrollLoadEnabled(true);

        listView.setOnRefreshListener(new OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                currentpage++;
                loadData(currentpage);
            }
        });
        listView.getRefreshableView().setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mContext, PostDetailActivity.class);
                intent.putExtra(Constants.KEY_POST_TID, arrays.get(position).tid);
                intent.putExtra(Constants.KEY_FROM_PAGE, 0);
                startActivity(intent);
            }

        });
    }

    private final LinkedList<Message> arrays = new LinkedList<Message>();
    private MyPostAdapter adapter;
    private String total;

    private void loadData(int currentPage) {
        RequestParams params = new RequestParams();
        params.put("uid", uid);
        params.put("page", String.valueOf(currentPage));
        params.put("pagesize", Address.PAGESIZE);
        AddClient.post(Address.FABIAO, params, new AsyncHttpResponseHandler() {
            private List<Message> array;

            @Override
            public void onSuccess(int statusCode, String content) {
                try {
                    MyHuiFu huifu = JSON.parseObject(content, MyHuiFu.class);
                    total = huifu.getTotal();
                    array = huifu.getArray();
                } catch (Exception e) {

                }
                if (array != null && array.size() > 0) {
                    arrays.addAll(array);
                }
                if (array != null && array.size() > 0) {
                    array.clear();
                }
                if (adapter == null) {
                    adapter = new MyPostAdapter(arrays, mContext);
                    listView.getRefreshableView().setAdapter(adapter);
                } else {
                    adapter.notifyDataSetChanged();
                }
                if (Integer.parseInt(total) < 10) {
                    listView.setHasMoreData(false);
                    listView.setPullLoadEnabled(false);
                    listView.setPullRefreshEnabled(false);
                }
                onLoaded();
                fl.setVisibility(View.GONE);
                super.onSuccess(statusCode, content);
            }

            @Override
            public void onFailure(Throwable error, String content) {
                Toast.makeText(mContext, "加载失败,请检查网络状态", Toast.LENGTH_SHORT).show();
                fl.setVisibility(View.GONE);
                super.onFailure(error, content);
                onLoaded();
            }
        });
    }

    private void onLoaded() {
        listView.onPullDownRefreshComplete();
        listView.onPullUpRefreshComplete();
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}
